<template>
  <div class="page-wrapper">
    <div class="header-wrapper">
      <h2 class="top">
        <van-icon class="back-icon" @click="fanhui" name="arrow-left" size="30px" />
        <span class="title-text">{{servicePackage.packageName}}详情</span>
      </h2>
    </div>
    <div class="fuwu-kuai" v-for="item in servicePackageItems" :key="item.id">
      <div class="fuwu-k">
        <span style="font-size: 20px">{{item.itemName}}</span><br>
        <span style="font-size: 15px;color: #8c939d">剩余次数：<span style="font-size: 15px;color: red">{{item.remainingTimes}}</span>次</span><br>
        <button  v-if="item.fee" style="border-radius: 10px;color: #28D094;background-color: #EAFAF5;border:none;margin-top: 10px">免费项目</button>

        <button  v-else style="border-radius: 10px;color: #FA746B;background-color: #FEE3E1;border:none;margin-top: 10px">付费项目</button>
      </div>
      <div class="fujilu">
        <el-button type="primary" style="margin-top: 20px" @click="jinqu(item.itemName)">服务记录</el-button>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  data() {
    return {
      servicePackage: {},
      servicePackageItems: [],
      isLoading: true // 数据加载状态
    };
  },
  methods: {
    findAll() {
      this.isLoading = true; // 开始加载数据
      this.axios.get(`http://localhost:10086/api/service/service_package/findServicePackageAll`)
          .then(res => {
            console.log('请求响应:', res);
            if (res.data.code === 200) {
              this.servicePackage = res.data.data.servicePackage;
              this.servicePackageItems = res.data.data.servicePackageItem;
              console.log('数据:', this.servicePackageItem); // 打印医生数据，检查是否有 id 属性
            } else {
              this.$message.error("获取失败");
            }
          })
          .catch(error => {
            console.error('请求出错:', error);
            this.$message.error('网络请求出错，请检查网络或服务器状态');
          })
          .finally(() => {
            this.isLoading = false; // 数据加载完成
          });
    },
    fanhui() {
      this.$router.push('/my');
    },
    jinqu(itemName){
      localStorage.setItem("itemName", itemName)
      this.$router.push({path: '/followService', query: {itemName: itemName}});
    }
  },
  created() {
    const token = JSON.parse(localStorage.getItem("token"));
    if (!token) {
      this.$message.error('请先登录');
      setTimeout(() => {
        this.$router.push({ name: 'login' });
      }, 1000);
    }
    // 设置全局默认请求头
    this.axios.defaults.headers.common['token'] = token;
    this.findAll();
  }
};
</script>

<style scoped>
/* 样式部分保持不变 */
</style>

<style scoped>
.fujilu{
  float: left;
  width: 100px;
  height: 80px;
  margin-top: 20px;
  margin-left: 30px;
}
.fuwu-k{
  float: left;
  width: 150px;
  height: 80px;
  margin-top: 20px;
  margin-left: 20px;
}
.fuwu-kuai{
  width: 80%;
  height: 120px;
  background-color: white;
  margin-left: 10%;
  margin-top: 30px;
  border-radius: 10px;
}
.tu-pian1 img {
  width: 80px;
  height: 80px;
  border-radius: 20px;
}
.tu-pian img {
  width: 150px;
  height: 150px;
  float: left;
  border-radius: 30px;
  box-shadow: 0 0 10px rgba(0, 128, 255, 0.2);
}

.page-wrapper {
  min-height: 100vh;
  background-color: #F5F9FD;
}

.header-wrapper {
  background-color: white;
}

.top {
  display: flex;
  align-items: center;
  height: 50px;
  position: relative;
}

.back-icon {
  position: absolute;
  left: 10px;
}

.title-text {
  width: 100%;
  text-align: center;
}

img {
  /*background-color: red;*/
  width: 100%;
  height: 100%;
}

.loading {
  text-align: center;
  margin-top: 20px;
  color: #999;
}

.no-data {
  text-align: center;
  margin-top: 20px;
  color: #999;
}
</style>